package com.lqk.demo.concurrencytools.threadpool.executors.scheduledthread;

import lombok.extern.slf4j.Slf4j;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author lqk
 * @date 2022/1/10 15:30
 */
@Slf4j
public class TestSchedule {
    // 如何让每周四 18:00:00 定时执行任务
    public static void main(String[] args) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
        /*
            initialDelay : 代表当前时间和周四的时间差
            period : 一周的时间间隔
         */
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 获取周四时间
        LocalDateTime time = now.withHour(18).withMinute(0).withSecond(0).withNano(0).with(DayOfWeek.THURSDAY);
        // 如果当前时间 > 本周周四, 将时间改为下周周四
        if (now.isAfter(time)){
            time = time.plusWeeks(1);
        }
        int period = 1000 * 60 * 60 * 24 * 7;
        long initialDelay = Duration.between(now, time).toMillis();
        pool.scheduleAtFixedRate(()->{

        }, initialDelay,period, TimeUnit.MILLISECONDS);
    }
}
